package cn.car91.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import cn.car91.R;
import cn.car91.bean.ArticleComment;
import cn.car91.bean.ExposureComment;
import cn.car91.bean.RepairShopComment;

public class UserCommentAdapter extends ArrayAdapter {
    private int resource;
    private LayoutInflater layoutInflater;
    private List data;

    //static ImageManager imageManager;
    public List getData() {
        return data;
    }

    public void setData(List data) {
        this.data = data;
    }

    public void addData(List data) {
        this.data.addAll(data);
    }

    //private InitApplication config;

    public UserCommentAdapter(Context context, int resource) {
        super(context, resource);
        this.resource = resource;
        this.layoutInflater = LayoutInflater.from(context);
        //config = (InitApplication) context.getApplicationContext();
        this.data = new ArrayList<RepairShopComment>();

        //imageManager = new ImageManager(getContext(), "sdcard/car91/temp/repairshop");
    }

    public final class RepairShopCommentItem {
        public TextView id;
        public TextView title;
        public TextView commentEvaluation;
        public TextView commentContent;
        public TextView commentTime;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return data.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub
        RepairShopCommentItem commentItem = null;
        if (arg1 == null) {
            commentItem = new RepairShopCommentItem();

            arg1 = layoutInflater.inflate(resource, null);
            commentItem.id = (TextView) arg1.findViewById(R.id.item_id);
            commentItem.title = (TextView) arg1.findViewById(R.id.comment_name);
            commentItem.commentEvaluation = (TextView) arg1.findViewById(R.id.comment_evaluation);
            commentItem.commentContent = (TextView) arg1.findViewById(R.id.comment_content);
            commentItem.commentTime = (TextView) arg1.findViewById(R.id.comment_time);

            arg1.setTag(commentItem);
        } else {
            commentItem = (RepairShopCommentItem) arg1.getTag();
        }

        String clazz = getItem(arg0).getClass().getSimpleName();
        if (clazz.equals(RepairShopComment.class.getSimpleName())) {
            RepairShopComment comment = (RepairShopComment) getItem(arg0);
            commentItem.id.setText(comment.getRid());
            commentItem.title.setText(comment.getTitle());
            commentItem.commentContent.setText(comment.getContent());
            commentItem.commentTime.setText(DateFormat.format("MM-dd kk:mm", comment.getTime()));
            String evaluation = "其他";
            try {
                switch (comment.getScore()) {
                    case 0:
                        evaluation = "好评";
                        break;
                    case 1:
                        evaluation = "中评";
                        break;
                    case 2:
                        evaluation = "差评";
                        break;
                    default:
                        break;
                }
            } catch (Exception e) {
            }
            commentItem.commentEvaluation.setText(evaluation);
        } else if (clazz.equals(ArticleComment.class.getSimpleName())) {
            ArticleComment comment = (ArticleComment) getItem(arg0);
            commentItem.id.setText(comment.getAid().toString());
            commentItem.title.setText(comment.getTitle());
            commentItem.commentContent.setText(comment.getContent());
            commentItem.commentTime.setText(DateFormat.format("MM-dd kk:mm", comment.getTime()));
        } else if (clazz.equals(ExposureComment.class.getSimpleName())) {
            ExposureComment comment = (ExposureComment) getItem(arg0);
            commentItem.id.setText(comment.getEid().toString());
            commentItem.title.setText(comment.getTitle());
            commentItem.commentContent.setText(comment.getContent());
            commentItem.commentTime.setText(DateFormat.format("MM-dd kk:mm", comment.getTime()));
        }

        return arg1;
    }
}
